<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="预约标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入预约标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标室地区" prop="bidroomAreaName">
        <el-select v-model="queryParams.bidroomAreaId" clearable placeholder="请选择">
            <el-option
              v-for="item in companyareaList"
              :key="item.areaId"
              :label="item.areaName"
              :value="item.areaId">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用途" prop="purposeId">
        <el-select v-model="queryParams.purposeId" clearable placeholder="请选择">
              <el-option
                v-for="item in purposeList"
                :key="item.purpose_id"
                :label="item.purpose_name"
                :value="item.purpose_id">
              </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开标时间" prop="projectBidOpenTime">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:bidroomreservation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:bidroomreservation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:bidroomreservation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:bidroomreservation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bidroomreservationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预约ID" align="center" prop="id" />
      <el-table-column label="预约标题" align="center" prop="title" />
      <el-table-column label="标室名称" align="center" prop="bidroomName" />
      <el-table-column label="用途" align="center" prop="purposeName" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目负责人" align="center" prop="projectLeader" />
      <el-table-column label="所属部门" align="center" prop="projectDeptName" />
      <el-table-column label="开标时间" align="center" prop="projectBidOpenTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.projectBidOpenTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约人" align="center" prop="creater" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bidroomreservation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bidroomreservation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
            <el-form-item label="预约标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入预约标题" style="width: 740px"/>
            </el-form-item>
        </el-row>

        <el-row>
            <el-col :span="12">
              <el-form-item label="项目" prop="projectName">
                <el-input v-model="form.projectName" placeholder="请选择项目" @focus="handleProject" style="width: 280px"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用途" prop="purposeId">
                <el-select v-model="form.purposeId" @change="selectPurpose" clearable placeholder="请选择" style="width: 280px">
                      <el-option
                        v-for="item in purposeList"
                        :key="item.purpose_id"
                        :label="item.purpose_name"
                        :value="item.purpose_id">
                      </el-option>
                </el-select>
              </el-form-item>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="12">
              <el-form-item label="地区" prop="bidroomId">
                  <el-select v-model="form.bidroomAreaId" @change="selectBidroomArea" clearable placeholder="请选择" style="width: 280px">
                      <el-option
                        v-for="item in companyareaList"
                        :key="item.areaId"
                        :label="item.areaName"
                        :value="item.areaId">
                      </el-option>
                  </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="标室选择" prop="bidroomId">
                <el-select v-model="form.bidroomId" @change="selectBidroom" clearable placeholder="请选择" style="width: 280px">
                    <el-option
                      v-for="item in bidroomList"
                      :key="item.id"
                      :label="item.bidroomName"
                      :value="item.id">
                    </el-option>
                </el-select>
              </el-form-item>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="12">
              <el-form-item label="开始时间" prop="startTime">
                <el-date-picker clearable
                  v-model="form.startTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm"
                  placeholder="请选择开始时间" style="width: 280px">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束时间" prop="endTime">
                <el-date-picker clearable
                  v-model="form.endTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm"
                  placeholder="请选择结束时间" style="width: 280px">
                </el-date-picker>
              </el-form-item>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="12">
              <el-form-item label="开标时间" prop="projectBidOpenTime">
                <el-date-picker clearable
                  v-model="form.projectBidOpenTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm"
                  placeholder="请选择开标时间" style="width: 280px">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="集合时间" prop="expertAssembleDt" style="width: 280px">
                <el-date-picker clearable
                  v-model="form.expertAssembleDt"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm"
                  placeholder="请输入专家集合时间" style="width: 280px">
                </el-date-picker>
              </el-form-item>
            </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" :rows="4" style="width: 740px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 项目列表 -->
    <el-dialog :title="projectTitle" :visible.sync="projectOpen" append-to-body>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="项目编号" prop="projectCode">
          <el-input
            v-model="queryParams.projectCode"
            placeholder="请输入项目编号"
            clearable
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input
            v-model="queryParams.projectName"
            placeholder="请输入项目名称"
            clearable
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item label="项目部门" prop="projectDeptId">
          <treeselect v-model="queryParams.projectDeptId" :options="deptList" :show-count="true" placeholder="请选择归属部门" style="width: 130px"/>
        </el-form-item>
        <el-form-item label="资质主体" prop="projectAgencyId">
          <el-input
            v-model="queryParams.projectAgencyId"
            placeholder="请输入资质主体"
            clearable
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="projectList">
        <el-table-column label="ID" align="center" prop="id"  width="55"/>
        <el-table-column label="项目名称" align="center" prop="projectName" />
        <el-table-column label="项目编号" align="center" prop="projectCode" />
        <el-table-column label="项目外部编号" align="center" prop="projectExternaltCode" />
        <el-table-column label="项目类型" align="center" prop="projectTypeName" />
        <el-table-column label="项目所属部门" align="center" prop="projectDeptName" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleSelectProject(scope.row)"
              v-hasPermi="['system:project:edit']"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="projectTotal>0"
        :total="projectTotal"
        :page.sync="projectParams.pageNum"
        :limit.sync="projectParams.pageSize"
        @pagination="getProjectList"
      />
    </el-dialog>


  </div>
</template>

<script>
import { myListBidroomreservation, getBidroomreservation, delBidroomreservation, addBidroomreservation, updateBidroomreservation } from "@/api/system/bidroomreservation";
import { listsCompanyarea } from "@/api/system/companyarea";
import { listsBidroom } from "@/api/system/bidroom";
import { listProject} from "@/api/system/project";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Bidroomreservation",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目总条数
      projectTotal: 0,
      // 预约管理表格数据
      bidroomreservationList: [],
      // 地区列表
      companyareaList: [],
      // 标识列表
      bidroomList: [],
      // 项目列表
      projectList: [],
      // 部门列表
      deptList: [],
      // 日期范围
      dateRange: [],
      // 用途
      purposeList: [
        {
          purpose_id: '1',
          purpose_name: '开标'
        },
        {
          purpose_id: '2',
          purpose_name: '评标'
        },
        {
          purpose_id: '3',
          purpose_name: '其他'
        },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 项目弹出层标题
      projectTitle: "",
      // 项目是否显示弹出层
      projectOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bidroomId: null,
        bidroomName: null,
        bidroomTypeId: null,
        bidroomType: null,
        bidroomAddr: null,
        bidroomAreaId: null,
        bidroomAreaName: null,
        projectId: null,
        projectName: null,
        projectLeader: null,
        projectLeaderId: null,
        projectDeptId: null,
        projectDeptName: null,
        projectAreaName: null,
        projectAreaId: null,
        projectAgencyId: null,
        projectAgencyName: null,
        projectBidOpenTime: null,
        purposeId: null,
        purposeName: null,
        title: null,
        startTime: null,
        endTime: null,
        expertAssembleDt: null,
        creater: null,
        createId: null,
      },
      projectParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCompanyAreaList();
    this.getBidroomList();
    this.getProjectList();
    this.getDeptTree();
  },
  methods: {
    /** 查询预约管理列表 */
    getList() {
      this.loading = true;
      myListBidroomreservation(this.addDateRange(this.queryParams, this.dateRange,'ProjectBidOpenTime')).then(response => {
        this.bidroomreservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bidroomId: null,
        bidroomName: null,
        bidroomTypeId: null,
        bidroomType: null,
        bidroomAddr: null,
        bidroomAreaId: null,
        bidroomAreaName: null,
        projectId: null,
        projectName: null,
        projectLeader: null,
        projectLeaderId: null,
        projectDeptId: null,
        projectDeptName: null,
        projectAreaName: null,
        projectAreaId: null,
        projectAgencyId: null,
        projectAgencyName: null,
        projectBidOpenTime: null,
        purposeId: null,
        purposeName: null,
        title: null,
        startTime: null,
        endTime: null,
        expertAssembleDt: null,
        remark: null,
        creater: null,
        createId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约管理";
    },
    handleProject(){
      this.projectOpen = true;
      this.projectTitle = "选择项目";
      console.log(666666)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBidroomreservation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBidroomreservation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBidroomreservation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预约管理编号为"' + ids + '"的数据项？').then(function() {
        return delBidroomreservation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bidroomreservation/export', {
        ...this.queryParams
      }, `bidroomreservation_${new Date().getTime()}.xlsx`)
    },
    /**获取地区列表**/
    getCompanyAreaList(){
      this.loading = true;
      listsCompanyarea(this.queryParams).then(response => {
        this.companyareaList = response.data;
        this.loading = false;
      });
    },
    getProjectList(){
      this.loading = true;
      listProject(this.projectParams).then(response => {
        this.projectList = response.rows;
        this.projectTotal = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptList = response.data;
      });
    },
    selectPurpose(value) {
      // 通过已选的value值找到对应的对象
      const selectedOption = this.purposeList.find(option => option.purpose_id === value);
      if (selectedOption) {
        this.form.purposeName = selectedOption.purpose_name;
      }
    },
    selectBidroomArea(value) {
      // 通过已选的value值找到对应的对象
      const selectedOption = this.companyareaList.find(option => option.areaId === value);
      if (selectedOption) {
        this.form.bidroomAreaName = selectedOption.areaName;
      }
    },
    getBidroomList() {
      this.loading = true;
      listsBidroom(this.queryParams).then(response => {
        this.bidroomList = response.data;
        this.loading = false;
      });
    },
    selectBidroom(value) {
      const selectedOption = this.bidroomList.find(option => option.id === value);
      if (selectedOption) {
        this.form.bidroomName = selectedOption.bidroomName;
        this.form.bidroomTypeId = selectedOption.bidroomTypeId;
        this.form.bidroomType = selectedOption.bidroomType;
      }
    },
    handleSelectProject(row){
      this.form.projectId = row.id;
      this.form.projectName = row.projectName;
      this.form.projectLeader = row.projectLeader;
      this.form.projectLeaderId = row.projectLeaderId;
      this.form.projectDeptId = row.projectDeptId;
      this.form.projectDeptName = row.projectDeptName;
      this.form.projectAreaId = row.projectAreaId;
      this.form.projectAreaName = row.projectAreaName;
      this.form.projectAgencyId = row.projectAgencyId;
      this.form.projectAgencyName = row.projectAgencyName;
      this.projectOpen=false;
    }
  }
};
</script>
